﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Base.Doc;
using UFIDA.U9.CBO.SCM.Enums;
using UFIDA.U9.PM.Rcv;
using UFSoft.UBF.Business;

namespace UFIDA.U9.Cust.YYD.PlugBE.ReceivementP
{
    public class AfterValidate : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.PM.Rcv.Receivement;
            if (holder == null)
                return;
            #endregion

            if (!string.IsNullOrEmpty(holder.DescFlexField.PrivateDescSeg1) && holder.OriginalData.DescFlexField.PrivateDescSeg1 != holder.DescFlexField.PrivateDescSeg1)
            {
                var ckdoc = Receivement.Finder.Find($"DescFlexField.PrivateDescSeg1 = '{holder.DescFlexField.PrivateDescSeg1}' and ID != {holder.ID}");
                if (ckdoc != null)
                {
                    throw new Exception($"WMS重复单号{ckdoc.DocNo}");
                }
            }
        }
    }
}
